import { CloundWatcher, FBSWatcher, TaskManage, LDCOnlineCheckTask, CloundLDCConfigTask, Watcher } from './taskManage';

export class LDCManager {
    storeId: string;
    LDCConfigValue: any;
    currentLDCConfigValue: any;
    currentLDCOnline: boolean;

    cloundWatcher: Watcher;
    fbsWatcher: Watcher;
    constructor(options: {
        storeId: string,
        LDCConfigValue: any
    }) {
        this.storeId = options.storeId;
        this.LDCConfigValue = options.LDCConfigValue;
        this.init();
    }

    init(){
        this.initLDCOnlineCheck();
    }

    compareLDCConfig(newLDCConfig) {
        return false;
    }
    initLDCOnlineCheck() {
        this.fbsWatcher = new FBSWatcher();
        TaskManage.current.addWatcher(this.fbsWatcher);
    }
    initTask() {

    }
    start() {
        if (!this.storeId) {
            throw new Error("storeId 没有初始化");
        }
        if (!this.LDCConfigValue) {
            throw new Error("LDCConfigValue 没有初始化");
        }
        const ldcOnlineCheckTask = new LDCOnlineCheckTask(this.storeId, this.LDCConfigValue.host);
        TaskManage.current.addTask(ldcOnlineCheckTask);
        const self = this;
        ldcOnlineCheckTask.onChecked = (status) => {
            if (self.currentLDCOnline != status) {
                self.onToggleLDCOnline(status);
                self.currentLDCOnline = status;
            }
        }
        this.fbsWatcher.start();


        // const cloundLDCConfig = new CloundLDCConfigTask(this.storeId);
        // cloundLDCConfig.onChecked = (ldcConfigValue) => {
        //     if (ldcConfigValue) {
        //         fbsLDCConfigTask.LDCHost = ldcConfigValue.host;
        //     }
        //     if (ldcConfigValue.host != this.LDCConfigValue.host) {
        //         this.onLDCConfigChange(ldcConfigValue.vue);
        //         this.LDCConfigValue = ldcConfigValue.value;
        //     }
        // }
    }
    stop() {
        
    }
    onToggleLDCOnline: (sender) => void
    onLDCConfigChange: (sender) => void
}